package com.ht.controller;

import com.alibaba.fastjson.JSON;
import com.ht.bean.*;
import com.ht.common.FileUpload;
import com.ht.dao.EmpDao;
import com.ht.service.EmpService;
import com.mysql.cj.Session;
import com.sun.xml.internal.ws.api.model.MEP;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.StringReader;
import java.util.*;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService ep;

    @Resource
    private EmpDao ed;

    //员工管理---------------------------------------------------------------------
    //去查看员工列表
    @RequestMapping("/toemp")
    public String toemp(Model model){
        //查询部门列表
        List<Dep> dlist = ep.deplist();
        model.addAttribute("dlist",dlist);
        //查询职务列表
        List<Post> plist = ep.postlist();
        model.addAttribute("plist",plist);
        return "emplist";
    }

    //查询员工
    @RequestMapping("/emplist")
    @ResponseBody
    public Map emplist(int page, int limit, Emp emp1,Model model){
        List<Map> list=ep.listEmp((page-1)*limit,limit,emp1);
        System.out.println(list.toString());
//        session.setAttribute("feedBackType","2");
//        session.setAttribute("empId","1");
//        session.setAttribute("stuId","1");
//        session.setAttribute("depId","1");
//        session.setAttribute("empname","大王");
//        session.setAttribute("teacher","5");
//        session.setAttribute("status","1");
        model.addAttribute("list",list);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",ep.countEmp(emp1));
        map.put("data",list);

        return map;
    }
    //去添加员工
    @RequestMapping("/toaddemp")
    public String tuaddemp(Model model){
//        List<Emp> elist = ep.empslist();
//        model.addAttribute("elist",elist);

        //查询部门列表
        List<Dep> dlist = ep.deplist();
        model.addAttribute("dlist",dlist);
        //查询职务列表
        List<Post> plist = ep.postlist();
        model.addAttribute("plist",plist);
        return "empadd";
    }

    //添加
    @RequestMapping("/addemp")
    @ResponseBody
    public Map addemp(Emp emp1, MultipartFile file, HttpSession session,String phone){
        String path = FileUpload.upload(file,session,"tp");
        emp1.setHeadImg(path);
        System.out.println(path);
        if (path!=null){
            System.out.println("151351241241");
            emp1.setHeadImg(path);
        }
        Map map = new HashMap();
//        boolean bln = ep.empadd(emp1);
        //判断手机号是否已拥有
        int a = ed.emp_phone(emp1.getPhone());
        map.put("a",a);
        System.out.println("a的值是-------"+a);

        int b = ed.emp_cardNo(emp1.getCardNo());
//        map.put("b",b);
        System.out.println("b的值是-------"+b);
        if(a==0){
            boolean bln = ep.empadd(emp1);
//            boolean bln = sg.adduser(user);
            if(bln){
                map.put("msg","添加成功");
            }else {
                map.put("msg","添加失败");
            }
        }else{
            map.put("msg","-");
        }
//        if(a==0 && b==0){
//            boolean bln = ep.empadd(emp1);
////            boolean bln = sg.adduser(user);
//            if(bln){
//                map.put("msg","添加成功");
//            }else {
//                map.put("msg","添加失败");
//            }
//        }else if(a==1){
//            map.put("msg","-");
//        }else if(b==1){
//            map.put("msg","+");
//        }
        return map;
    }



    //删除员工
    @RequestMapping("/delemp")
    @ResponseBody
    public Map delemp(Integer empId){
        Map map=new HashMap();
        boolean bln= ep.delemp(empId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }

    //去查看工作经历，教育背景，家庭联系关系，员工考核的ID---------------------------------------------------------------------------------------
    @RequestMapping("/toemplist/{empId}")
    public String toemplist(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        return "emptolist";
    }

    //查看工作经历
    @RequestMapping("/emptolist/{empId}")
    @ResponseBody
    public Map emptolist(@PathVariable("empId")Integer empId, Model model){
        List<Map> jlist = ep.joblist(empId);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",0);
        map.put("data",jlist);
        return map;
    }

    //去添加工作经历
    @RequestMapping("/toaddjob/{empId}")
    public String toaddjob(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        return "addjob";
    }

    //添加工作经历
    @RequestMapping("/addjob")
    @ResponseBody
    public Map addjob(Job job){
        Map map = new HashMap();
        boolean bln = ep.addjob(job);
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }

    //删除工作经历
    @RequestMapping("/deljob")
    @ResponseBody
    public Map deljob(Integer jobId){
        Map map=new HashMap();
        boolean bln= ep.deljob(jobId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }

    //查看教育背景---------------------------------------------------------------------------------
    @RequestMapping("/emptolist2/{empId}")
    @ResponseBody
    public Map emptolist2(@PathVariable("empId")Integer empId, Model model){
        List<Map> elsit = ep.edulist(empId);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",0);
        map.put("data",elsit);
        return map;
    }

    //去添加教育背景
    @RequestMapping("/toaddedu/{empId}")
    public String toaddedu(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        return "addedu";
    }

    //添加教育背景
    @RequestMapping("/addedu")
    @ResponseBody
    public Map addjob(Education education){
        Map map = new HashMap();
        boolean bln = ep.addedu(education);
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }

    //删除教育背景
    @RequestMapping("/deledu")
    @ResponseBody
    public Map deledu(Integer collegeId){
        Map map=new HashMap();
        boolean bln= ep.deledu(collegeId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }
    //查看家庭联系信息----------------------------------------------------------------------------------
    @RequestMapping("/emptolist3/{empId}")
    @ResponseBody
    public Map emptolist3(@PathVariable("empId")Integer empId, Model model){
        List<Map> flsit = ep.familylist(empId);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",0);
        map.put("data",flsit);
        return map;
    }

    //去添加家庭联系关系
    @RequestMapping("/toaddfamily/{empId}")
    public String toaddfamily(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        return "addfamily";
    }

    //添加家庭联系关系
    @RequestMapping("/addfamily")
    @ResponseBody
    public Map addfamily(Familyinfo familyinfo){
        Map map = new HashMap();
        boolean bln = ep.addfamily(familyinfo);
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }
    //删除家庭联系关系
    @RequestMapping("/delfamily")
    @ResponseBody
    public Map delfamily(Integer familyId){
        Map map=new HashMap();
        boolean bln= ep.delfamily(familyId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }

    //查看员工考核----------------------------------------------------------------------------------
    @RequestMapping("/emptolist4/{empId}")
    @ResponseBody
    public Map emptolist4(@PathVariable("empId")Integer empId, Model model){
        List<Map> llsit = ep.loglist(empId);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",0);
        map.put("data",llsit);
        return map;
    }

    //去添加员工考核
    @RequestMapping("/toaddlog/{empId}")
    public String toaddlog(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        //查询考核指标
        List<Map> mlist = ep.modellist();
        model.addAttribute("mlist",mlist);
        return "addlog";
    }

    //添加员工考核
    @RequestMapping("/addlog")
    @ResponseBody
    public Map addlog(Aduitlog aduitlog, MultipartFile file, HttpSession session){
        String path = FileUpload.upload(file,session,"tpp");
        aduitlog.setImage(path);
        Map map = new HashMap();
        boolean bln = ep.addlog(aduitlog);
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }

    //去查看考核详情
    @RequestMapping("/tolooklog/{aduitLogId}")
    public String tolooklog(@PathVariable("aduitLogId") Integer aduitLogId, Model model){
        Map log=ep.loglistbyid(aduitLogId);
        model.addAttribute("log", log);
//        session.setAttribute("log",log);
        return "loglist";
}

    //查看考勤详情
    @RequestMapping("/loglist")
    @ResponseBody
    public Map loglist(@PathVariable("aduitLogId") Integer aduitLogId,Model model){
        Map llist=ep.loglistbyid(aduitLogId);
        model.addAttribute("llist",JSON.toJSON(llist));
//        System.out.println(llist);
        Map map=new HashMap();
        map.put("code",0);
//        map.put("mgs","成功");
//        map.put("count",0);
        map.put("data",llist);
        return map;
    }

    //删除员工考核
    @RequestMapping("/dellog")
    @ResponseBody
    public Map dellog(Integer aduitLogId){
        Map map=new HashMap();
        boolean bln= ep.dellog(aduitLogId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }

    //查看员工证件----------------------------------------------------------------------------------
    @RequestMapping("/emptolist5/{empId}")
    @ResponseBody
    public Map emptolist5(@PathVariable("empId")Integer empId, Model model){
        List<Map> plsit = ep.paperlist(empId);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",0);
        map.put("data",plsit);
        return map;
    }

    //去添加员工考核
    @RequestMapping("/toaddpaper/{empId}")
    public String toaddpaper(Model model,@PathVariable("empId") Integer empId){
        model.addAttribute("empId",empId);
        return "addpaper";
    }

    //添加员工证件
    @RequestMapping("/addpaper")
    @ResponseBody
    public Map addpaper(Paper paper, MultipartFile file, HttpSession session){
        String path = FileUpload.upload(file,session,"tp");
        paper.setPapers(path);
        Map map = new HashMap();
        boolean bln = ep.addpaper(paper);
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }

    //删除员工证件
    @RequestMapping("/delpaper")
    @ResponseBody
    public Map delpaper(Integer imgId){
        Map map=new HashMap();
        boolean bln= ep.delpaper(imgId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }

    //去查看员工证件详情
    @RequestMapping("/tolookpaper/{imgId}")
    public String tolookpaper(@PathVariable("imgId") Integer imgId, Model model){
        Map paper=ep.paperlistbyid(imgId);
        model.addAttribute("paper", paper);
//        session.setAttribute("log",log);
        return "paperlist";
    }

    //查看考勤详情
    @RequestMapping("/paperlist")
    @ResponseBody
    public Map paperlist(@PathVariable("aduitLogId") Integer imgId,Model model){
        Map plist=ep.paperlistbyid(imgId);
        model.addAttribute("plist",JSON.toJSON(plist));
        Map map=new HashMap();
        map.put("code",0);
        map.put("data",plist);
        return map;
    }

    //去修改员工------------------------------------------------------------------
    @RequestMapping("/toupdemp/{empId}")
    public String toupdemp(@PathVariable("empId") Integer empId, Model model){
        Emp emp=ep.updempbyid(empId);
        model.addAttribute("emp", JSON.toJSON(emp));
        List<Map> elist = ep.emplist();
        model.addAttribute("elist",elist);
        //查询部门列表
        List<Dep> dlist = ep.deplist();
        model.addAttribute("dlist",dlist);
        //查询职务列表
        List<Post> plist = ep.postlist();
        model.addAttribute("plist",plist);
        return "empupd";
    }

    //修改员工
    @RequestMapping("/empupd")
    @ResponseBody
    public Map updstu(Emp emp1,MultipartFile file, HttpSession session){
        System.out.println("++++"+emp1.getHeadImg());
        String path = FileUpload.upload(file,session,"tp");
        if (path!=null){
            emp1.setHeadImg(path);
        }
        System.out.println("---------"+emp1.getHeadImg());
        boolean bln=ep.updemp(emp1);
        Map map=new HashMap();
        if(bln){
            map.put("msg","修改成功！");
        }else {
            map.put("msg","修改失败！");
        }

        return map;
    }

    //重置密码
    @RequestMapping("/empczmm")
    @ResponseBody
    public Map empczmm(Integer empId){
        Map map=new HashMap();
        boolean bln= ep.empczmm(empId);
        if(bln){
            map.put("msg","重置成功！");
        }else{
            map.put("msg","重置失败！");
        }
        return map;
    }

    //周报--------------------------------------------------------------------
    //去查看周报
    @RequestMapping("/toweekly")
    public String toweekly(Model model){
        //查询部门列表
        List<Dep> dlist = ep.deplist();
        model.addAttribute("dlist",dlist);
        return "weeklylist";
    }

    //查看周报
    @RequestMapping("/weekly")
    @ResponseBody
    public Map weekly(Integer page, Integer limit,Weekly weekly){
        List<Map> list=ep.weeklylsit((page-1)*limit,limit,weekly);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",ep.countWeekly(weekly));
        map.put("data",list);
        return map;
    }

    //去查看
    @RequestMapping("/tolookweekly/{worklogId}")
    public String tolookweekly(@PathVariable("worklogId") Integer worklogId, Model model){
        Map weekly=ep.weeklybyId(worklogId);
        model.addAttribute("weekly", JSON.toJSON(weekly));

        return "zhoubao";
    }


    //查看详情
    @RequestMapping("/zhoubao")
    @ResponseBody
    public Map zhoubao(Integer page, Integer limit,Weekly weekly){
        List<Map> list=ep.weeklylsit((page-1)*limit,limit,weekly);
        System.out.println(list);
        Map map=new HashMap();

        return map;
    }



    //谈心内容----------------------------------------------------------------------
    //去查看谈心内容
    @RequestMapping("/tocord")
    public String tocord(Model model){
        //查询学生名单
        List<Map> slist = ep.stulist();
        model.addAttribute("slist",slist);
        //查询员工名单
        List<Map> elist = ep.emplist();
        model.addAttribute("elist",elist);
        return "chatrecord";
    }

    //查看谈心日志
    @RequestMapping("/cord")
    @ResponseBody
    public Map cord(HttpSession session,Integer page, Integer limit,Chatrecord chatrecord,Model model){
        List<Map> list=ep.cordlist((page-1)*limit,limit,chatrecord);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",ep.countCord(chatrecord));
        map.put("data",list);
        return map;
    }
    //添加谈心记录
    @RequestMapping("/addcord")
    @ResponseBody
    public Map addcord(Chatrecord chatrecord){
        Map map = new HashMap();
        boolean bln = ep.addCord(chatrecord);
        System.out.println(chatrecord.toString());
        if (bln){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }

    //删除谈心日志
    @RequestMapping("/delcord")
    @ResponseBody
    public Map delecord(Integer chatId){
        Map map=new HashMap();
        boolean bln= ep.delcord(chatId);
        if(bln){
            map.put("msg","删除成功！");
        }else{
            map.put("msg","删除失败！");
        }
        return map;
    }


    //-----------------------------------------------------------------------------------------------
    //系统报表
    //历史值班表
    @RequestMapping("/toange")
    public String toange(Model model){
        //查询员工
        List<Emp> elist = ep.empslist();
        model.addAttribute("elsit",elist);

        return "angebiao";
    }

    //查询历史值班表
    @RequestMapping("/ange")
    @ResponseBody
    public Map ange(Integer page, Integer limit,Weekarrange weekarrange){
        List<Map> list=ep.allange((page-1)*limit,limit,weekarrange);
        Map map=new HashMap();
        map.put("code",0);
        map.put("mgs","成功");
        map.put("count",ep.countange(weekarrange));
        map.put("data",list);
        return map;
    }
        //去查看历史值班统计图
    @RequestMapping("/toangeban")
    public String toangeban(){

        return "angban";

    }
    //查看查看历史值班统计图
    @RequestMapping("/countMap")
    @ResponseBody
    public Map countMap(){
        Map map = new HashMap();
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
//        List<Integer> list1 = new ArrayList<>();
        List<Weekarrange> wlist = ep.countMapName();
        for (Weekarrange wlists : wlist){
            list.add(wlists.getEmpName());
            list2.add(wlists.getEmpId());
        }

        map.put("EmpName",list);
        map.put("countNum",list2);
        System.out.println(list.toString());
        System.out.println(list2.toString());

        return map;
    }
}
